package com.sdut.encryption.controller;

import com.sdut.codeutil.IVerifyCodeGen;
import com.sdut.codeutil.SimpleCharVerifyCodeGenImpl;
import com.sdut.codeutil.VerifyCode;
import com.sdut.encryption.pojo.Manager;
import com.sdut.encryption.pojo.User;
import com.sdut.encryption.service.ManagerService;
import com.sdut.encryption.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Locale;

/**
 * 登录页等跳转的controller
 * @author 沐辰
 */
@Controller
public class IndexController {
    @Autowired
    ManagerService managerService;
    @Autowired
    UserService userService;
    @RequestMapping("AdminLogin")
    public String toAdminLogin(){
        return "adminLogin";
    }

    /**
     * 主页（登录页）
     * @return
     */
    @RequestMapping("/index")
    public String toIndex() { return "index";}

    /**
     * 测试页面（要删除）
     * @return
     */
    @RequestMapping("/test")
    public String toTest() { return "test";}

    /**
     * 登录检测
     */
    @RequestMapping("/login")
    public String doLogin(HttpServletRequest request){
        //获取用户名密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //获取输入的验证码
        String code = request.getParameter("captcha");
        //获取用户类型
        String type = request.getParameter("type");
        //判断验证码是否正确
        HttpSession session = request.getSession();
        String realCode = (String) session.getAttribute("VerifyCode");
        if(!realCode.toLowerCase(Locale.ROOT).equals(code.toLowerCase(Locale.ROOT))){
            session.setAttribute("msg","验证码错误!");
            return "redirect:index";
        }
        //管理员登录
        if(type.equals("1")){
            Manager manager = managerService.findOne(username, password);
            if (manager!=null){
                session.removeAttribute("msg");
                session.setAttribute("manager",manager);
                return "test";
            }
            else{
                session.setAttribute("msg","用户名或密码错误！");
                return "redirect:index";
            }
        }
        //用户登录
        else{
            User user = userService.findOne(username, password);
            if(user!=null){
                session.removeAttribute("msg");
                session.setAttribute("user",user);
                return "test";
            }
            else{
                session.setAttribute("msg","用户名或密码错误！");
                return "redirect:index";
            }
        }
    }

    /**
     * 验证码获取
     * @param request
     * @param response
     */
    @RequestMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();
            //将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            System.out.println("异常处理");
        }
    }
}
